 const app = getApp();

Page({
  data: {
    result: ''
  },

  getscancode() {
    const _this = this;
    wx.scanCode({
      success: (res) => {
        let codeContent = res.result;
    // 接下来解析 codeContent 中的 codeId
        let codeId = _this.parseCodeId(codeContent);
    // 使用 codeId 进行后续处理
        _this.handleCodeId(codeId);
      },
      fail: (err) => {
        console.error(err);
      }
    });
  },

  parseCodeId:function(codeContent) {
    let codeId = this.parseQueryString(codeContent);
    console.log(codeId)
    return codeId;
  },

  handleCodeId:function(codeId)
  {
    console.log(codeId+"   =====")
    const _this = this;
    if(codeId)
    {
      wx.request({
        url: app.serverUrl+ 'readQRCode',
        method: 'POST',
        data:{
          qrCodeId:codeId
        },
        success(res)
        {
          const data = res.data
          if(data.code === 200)
          {
             //跳转到展示界面
             _this.jumpToShow(codeId)
          }
          else if(data.code === 220 )
          {
            //跳转到创作界面
            wx.navigateTo({
              url: '/pages/create/create',
              success: function(res) {
                // 通过 res.eventChannel 传递数据
                res.eventChannel.emit('acceptDataFromOpenerPage', { 
                  code: codeId,
                });
              },
              fail: function(error)
              {
                console.log(error)
              }
            });
          }
          else{

            wx.showToast({
              title: '无效的二维码',
              icon: 'none'
            })
           
          }
        }
      })

    }else
    {
      wx.showToast({
        title: '无效的二维码',
        icon: 'none'
      })
    }
  },

  parseQueryString:function(url) {
    // 首先判断 url 是否包含问号
    if (url.indexOf('?') === -1) {
      return {};
    }
  
    // 提取问号后的部分
    const queryString = url.split('?')[1];
  
    // 将查询字符串分割为键值对数组
    const pairs = queryString.split('&');
    // 将数组转换为对象

    let v= ''

    for (let i = 0; i < pairs.length; i++) {
      const pair = pairs[i].split('=');
      const key = decodeURIComponent(pair[0]);
      v = decodeURIComponent(pair[1] || '');

    }
  
    return v;
  },
  

  jumpToShow:function(codeId)
  {
    wx.request({
      url: app.serverUrl+'qrcode/send',
      method:'GET',
      header:{
        qrcode: codeId
      },
      success(res){
        console.log(res.data)
        const data = res.data;
        if(data.code === 200)
        {
          wx.navigateTo({
            url: '/pages/show/show',
            success: function(res) {
              // 通过 res.eventChannel 传递数据
              res.eventChannel.emit('acceptDataFromOpenerPage', { 
                imageList: data.data.picturePaths,
                videoList: data.data.videoPaths,
                audioPath: data.data.voicePath,
                textContent: data.data.content,
                recipient: data.data.toName,
                Sender: data.data.userName,
                Time: data.data.date
              });
            },
            fail: function(error)
            {
              console.log(error)
            }
          });

        }
      },
      fail(error){
        console.log(error)
      }
    })
  },

  

  onLoad(options) {},

  onReady() {},

  onShow() {},

  onHide() {},

  onUnload() {},

  onPullDownRefresh() {
    this.setData({
      result: ''
    });
    wx.stopPullDownRefresh();
  },

  onReachBottom() {},

  onShareAppMessage() {}
});
